function addOpenSearch()
{
 // ripped out of mycroft lol
  if ((typeof window.external == "object") && ((typeof window.external.AddSearchProvider == "unknown") || (typeof window.external.AddSearchProvider == "function"))) {
    // See bugs 430058/430067/430070 for Camino
    if (((typeof window.external.AddSearchProvider == "unknown") || (window.navigator.vendor == 'Camino'))&& meth == "p") {
      alert("This plugin uses POST which is not currently supported by your browser's implementation of OpenSearch.");
    } else {
      window.external.AddSearchProvider(window.base+"sambaindex.xml");
    }
  } else {
    alert("You will need a browser which supports OpenSearch to install this plugin.");
  }
}


var search = {
	terms: [],
 	inited: 0,
 	
 	searchtypes: {
 		'naturalsearch':'Natural Search',
 		'simplesearch':'Simple Search',
 		'awesomeform':'Awesome Search'
 	},
 	searchtypedivs: [],
  
 	init: function()
	{
		if (search.inited==0)
		{
			window.base=location.href.substr(0,location.href.lastIndexOf('/')+1);
			
			if (document.getElementById('addsearchprovider'))
			{
				document.getElementById('addsearchprovider').style.display="inline";
			}
			
			search.createTabs('searchtypes');
			if (document.getElementById("awesomeform"))
				search.createAwesomeSearch();

			var forms=['naturalsearchform','simplesearchform','awesomesearchform'];
			for (var id in forms)
			{
				id=forms[id];
				var element=document.getElementById(id);
				if (element.addEventListener)
					window.addEventListener("submit",search.handleSearch,false);
				if (element.attachEvent)
					window.attachEvent("onsubmit",search.handleSearch);
			}


			search.inited=1;
		}
	},
	
	createTabs: function(parentid)
	{
		var container = document.getElementById(parentid);
		
		var tabcontainer = document.createElement('span');
		tabcontainer.id='tabcontainer';
		
		if (container.childNodes.length>0)
			container.insertBefore(tabcontainer,container.childNodes[0]);
		else
				container.appendChild(tabcontainer);
				
		var firstselector=null;
		for (var i=0; i<container.childNodes.length; i++)
		{
			var curdiv=container.childNodes[i];
			
			if (curdiv!=tabcontainer && curdiv.tagName && curdiv.tagName.toLowerCase()=='div')
			{
				curdiv.style.display='none';
				search.searchtypedivs.push(curdiv);
				
				var selector = document.createElement('span');
				selector.innerHTML=search.searchtypes[curdiv.id];
				selector.targetdiv=curdiv;
				selector.onclick=search.showtab;
				
				tabcontainer.appendChild(selector);
				
				if (!firstselector)
					firstselector=selector;
			}
		}
		firstselector.onclick();
	},
 
	createAwesomeSearch: function(parentid)
	{
		search.appendAwesomeSearchTerm();
	},
 
 	appendAwesomeSearchTerm: function()
	{
		num=search.terms.length;
		
		// create the form part for the next search term
		var term = document.createElement('div');
		term.id="searchterm"+num;
		
		// whether the term is matched against file or folder name
		var termscope = document.createElement('select');
		termscope.setAttribute('name','scope[]');
		var termscope1 = document.createElement('option');
		termscope1.innerHTML="Folder name";
		termscope1.setAttribute('value','folder');
		var termscope2 = document.createElement('option');
		termscope2.innerHTML="File name";
		termscope2.setAttribute('value','file');
		var termscope3 = document.createElement('option');
		termscope3.setAttribute('selected','selected');
		termscope3.innerHTML="Folder name or file name";
		termscope3.setAttribute('value','folderfile');
		
		// how the term is matched
		var termpred = document.createElement('select');
		termpred.setAttribute('name','pred[]');
		var termpred1 = document.createElement('option');
		termpred1.innerHTML="contains";
		termpred1.setAttribute('value','contains');
		var termpred2 = document.createElement('option');
		termpred2.innerHTML="does not contain";
		termpred2.setAttribute('value','notcontains');
		var termpred3 = document.createElement('option');
		termpred3.innerHTML="starts with";
		termpred3.setAttribute('value','starts');
		var termpred4 = document.createElement('option');
		termpred4.innerHTML="ends with";
		termpred4.setAttribute('value','ends');
		
		// what the term is
		var termtext = document.createElement('input');
		termtext.setAttribute('name','query[]');
		termtext.id="searchterm"+num+"text";
		
		// create a link to make more terms
		var termlink = document.createElement('a');
		termlink.setAttribute('href','javascript:search.appendAwesomeSearchTerm("awesomeform")');
		termlink.innerHTML="Add more";
		
		// put the options into the select forms
		termscope.appendChild(termscope1);
		termscope.appendChild(termscope2);
		termscope.appendChild(termscope3);
		termpred.appendChild(termpred1);
		termpred.appendChild(termpred2);
		termpred.appendChild(termpred3);
		termpred.appendChild(termpred4);
		
		// add the parts of the form into the row
		term.appendChild(termscope);
		term.appendChild(termpred);
		term.appendChild(termtext);
		term.appendChild(termlink);
		
		search.terms[num]=term;
		
		// insert the term row
		var node = document.getElementById('awesome_terms');
		node.appendChild(term);	// before the submit field
		var nodes=node.getElementsByTagName('div');
		// add an AND/OR field to the previous term, if needed
		if (nodes.length>1)	// has more than one term,  and the submit divs
		{
			var lastterm = nodes[nodes.length-2]; 	// before the last term
			var link = lastterm.getElementsByTagName('a');
			if (link.length==1)	// if have a link to add a new term
			{
				var termjoin = document.createElement('select');
				termjoin.setAttribute('name','queryjoin[]');
				var termjoin1 = document.createElement('option');
				termjoin1.innerHTML="AND";
				termjoin1.setAttribute('value','and');
				var termjoin2 = document.createElement('option');
				termjoin2.innerHTML="OR";
				termjoin2.setAttribute('value','or');

				termjoin.appendChild(termjoin1);
				termjoin.appendChild(termjoin2);
				
				link=link[0];
				lastterm.replaceChild(termjoin,link);
			}
		}
	},
	
	showtab: function()
	{
		var selectors=this.parentNode.childNodes;
		for (var dividx=0; dividx<selectors.length; dividx++)
		{
			selectors[dividx].className="background";
			if (selectors[dividx]==this)
				this.className="foreground";
		}

		for (var dividx=0; dividx<search.searchtypedivs.length; dividx++)
		{
			search.searchtypedivs[dividx].style.display='none';
		}
		this.targetdiv.style.display='block';
	},

	handleSearch: function()	
	{
		var throbber=document.getElementById('throbber');
		throbber.style.display="inline";
	}
}
if (window.addEventListener)
	window.addEventListener("load",search.init,false);
if (window.attachEvent)
	window.attachEvent("onload",search.init);
